﻿using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace XieCan.Core.Swigger.filters
{
  internal class SchemaFilter : ISchemaFilter
  {
    void ISchemaFilter.Apply(OpenApiSchema schema, SchemaFilterContext context)
    {
      if (schema == null || schema.Properties.Count == 0)
        return;

      foreach (var property in schema.Properties)
      {
        var defaultValueAttribute = context.ParameterInfo?.GetCustomAttribute<DefaultValueAttribute>();
        if (defaultValueAttribute != null)
        {
          property.Value.Default = (IOpenApiAny?)defaultValueAttribute.Value;
        }
        else if (property.Key.Equals("page", StringComparison.CurrentCultureIgnoreCase))
        {
          property.Value.Default = new OpenApiInteger(1);
        }
        else if (property.Key.Equals("size", StringComparison.CurrentCultureIgnoreCase))
        {
          property.Value.Default = new OpenApiInteger(10);
        }
      }
    }
  }
}













